$(function () {

    var result = Model.shopcart.findAllShopcartItemByUserid({
        id: '',
        pageSize: 10,
        page: 1
    })
    var shopMarket = [{
        shopName: "app产品专营店1",
        isFreeSend: false,
        shopList: [{
            id: "1",
            name: "新疆苹果",
            note: "50斤",
            price: 10.00,
            count: 1,
            isSelect: false
        }
        ]
    }, {
        shopName: "app产品专营店2",
        isFreeSend: false,
        shopList: [{
            id: "3",
            name: "新疆苹果",
            note: "50斤",
            price: 30.00,
            count: 1,
            isSelect: false
        }
        ]
    }];
    showList();

    //显示
    function showList() {
        calculate();
        isAllchoose();
        var lhtml = "";
        for (var i = 0; i < shopMarket.length; i++) {
            lhtml += '<div class="commodity_box">'
            lhtml += '<div class="commodity_list">'
            lhtml += '<div class="tite_tim select">'
            lhtml += '<em aem="1" cart_id="84"></em>'
            lhtml += '<span>' + shopMarket[i].shopName + '</span>'
            lhtml += ' <div class="clear"></div>'
            lhtml += '</div>'
            lhtml += '<ul class="commodity_list_term">'
            lhtml += '<li class="select">'
            if (shopMarket[i].shopList[0].isSelect) {
                lhtml += ' <em onclick="choose(' + shopMarket[i].shopList[0].id + ')" aem="0" cart_id="84" class="pitch_on"></em>'
            } else {
                lhtml += ' <em aem="0" onclick="choose(' + shopMarket[i].shopList[0].id + ')" cart_id="84"></em>'
            }
            lhtml += ' <img src="../img/sp1.jpg" />'
            lhtml += ' <div class="div_center">'
            lhtml += ' <h4>' + shopMarket[i].shopList[0].name + '</h4>'
            lhtml += ' <span>' + shopMarket[i].shopList[0].note + '</span>'
            lhtml += '<p class="now_value">'
            lhtml += '<i>￥</i>'
            lhtml += '<b class="qu_su">' + shopMarket[i].shopList[0].price + '</b>'
            lhtml += '</p>'
            lhtml += '</div>'
            lhtml += ' <div class="div_right">'
            lhtml += '<i onclick="reducew(' + shopMarket[i].shopList[0].id + ')">-</i>'
            lhtml += '<span class="zi">' + shopMarket[i].shopList[0].count + '</span>'
            lhtml += '<input type="hidden" value="84">'
            lhtml += '<i onclick="plusw(' + shopMarket[i].shopList[0].id + ')">+</i>'
            lhtml += '</div>'
            lhtml += '</li>'
            lhtml += '</ul>'
            lhtml += ' </div>'
            lhtml += '</div>'
        }
        $("#shoplist").empty().append(lhtml);

    }

    //计算价格
    function calculate() {
        var count = 0;
        for (var i = 0; i < shopMarket.length; i++) {
            for (var j = 0; j < shopMarket[i].shopList.length; j++) {
                if (shopMarket[i].shopList[0].isSelect) {
                    var shopP = shopMarket[i].shopList[0].count * shopMarket[i].shopList[0].price;
                    count += shopP;
                }
            }
        }
        $("#total_price b").text(count)

    }

    //减
    function reducew(id) {
        for (var i = 0; i < shopMarket.length; i++) {
            for (var j = 0; j < shopMarket[i].shopList.length; j++) {
                if (id == shopMarket[i].shopList[0].id && shopMarket[i].shopList[0].count > 1) {
                    shopMarket[i].shopList[0].count--;

                }
            }
        }
        showList();
    }

    //加
    function plusw(id) {
        for (var i = 0; i < shopMarket.length; i++) {
            for (var j = 0; j < shopMarket[i].shopList.length; j++) {
                if (id == shopMarket[i].shopList[0].id) {
                    shopMarket[i].shopList[0].count++;

                }
            }
        }
        showList();
    }

    //单选
    function choose(id) {
        for (var i = 0; i < shopMarket.length; i++) {
            for (var j = 0; j < shopMarket[i].shopList.length; j++) {
                if (id == shopMarket[i].shopList[0].id) {
                    shopMarket[i].shopList[0].isSelect = !shopMarket[i].shopList[0].isSelect;
                }
            }
        }
        isAllchoose();
        showList();
    }

    //判断是否全选
    function isAllchoose() {
        var isAllSec = true;
        for (var i = 0; i < shopMarket.length; i++) {
            for (var j = 0; j < shopMarket[i].shopList.length; j++) {
                if (!shopMarket[i].shopList[0].isSelect) {
                    isAllSec = false;
                }
            }
        }
        if (isAllSec) {
            $("#all_pitch_on").addClass("pitch_on")
        } else {
            $("#all_pitch_on").removeClass("pitch_on")
        }
        return isAllSec;
    }


    //全选
    function allChoose() {

        if (isAllchoose()) {
            $("#all_pitch_on").removeClass("pitch_on")
            for (var i = 0; i < shopMarket.length; i++) {
                for (var j = 0; j < shopMarket[i].shopList.length; j++) {

                    shopMarket[i].shopList[0].isSelect = false;

                }
            }
        } else {
            $("#all_pitch_on").addClass("pitch_on")
            for (var i = 0; i < shopMarket.length; i++) {
                for (var j = 0; j < shopMarket[i].shopList.length; j++) {

                    shopMarket[i].shopList[0].isSelect = true;

                }
            }
        }
        showList();
    }

    // var pullRefresh = new auiPullToRefresh({
    //     container: document.querySelector('.aui-refresh-content'),
    //     triggerDistance: 100
    // }, function (ret) {
    //     if (ret.status == "success") {
    //         setTimeout(function () {
    //             console.log('刷新')
    //             var n = shopMarket.length + 1;
    //
    //             var wrap = document.getElementById("demo")
    //             var shop = {
    //                 shopName: "app产品专营店" + n,
    //                 isFreeSend: false,
    //                 shopList: [{
    //                     id: (n - 1) * 2 + 1,
    //                     name: "新疆苹果",
    //                     note: "50斤",
    //                     price: 30.00,
    //                     count: 1,
    //                     isSelect: false
    //                 },
    //                     {
    //                         id: (n - 1) * 2 + 2,
    //                         name: "新疆苹果",
    //                         note: "50斤",
    //                         price: 40.00,
    //                         count: 1,
    //                         isSelect: false
    //                     }
    //                 ]
    //             };
    //             var arr = new Array();
    //             arr = shopMarket;
    //
    //             arr.push(shop);
    //             showList();
    //             pullRefresh.cancelLoading(); //刷新成功后调用此方法隐藏
    //         }, 1500)
    //     }
    // })

});